package com.xteam.xnetworkdisk.sys.service.impl;

import com.xteam.xnetworkdisk.common.CurrentUser;
import com.xteam.xnetworkdisk.utils.PageUtils;
import com.xteam.xnetworkdisk.utils.Query;
import com.xteam.xnetworkdisk.utils.R;
import com.xteam.xnetworkdisk.vo.SaveContactsVO;
import com.xteam.xnetworkdisk.vo.UpdateContactsVO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.xteam.xnetworkdisk.sys.dao.ContactsDao;
import com.xteam.xnetworkdisk.sys.entity.ContactsEntity;
import com.xteam.xnetworkdisk.sys.service.ContactsService;


@Service("contactsService")
public class ContactsServiceImpl extends ServiceImpl<ContactsDao, ContactsEntity> implements ContactsService {

    private final ContactsDao contactsDao;

    public ContactsServiceImpl(ContactsDao contactsDao) {
        this.contactsDao = contactsDao;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ContactsEntity> page = this.page(
                new Query<ContactsEntity>().getPage(params),
                new QueryWrapper<ContactsEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R contactsList() {
        Integer currentUserId = CurrentUser.getCurrentUserId();
        QueryWrapper<ContactsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", currentUserId);
        List<ContactsEntity> contactsEntities = contactsDao.selectList(wrapper);
        return R.ok().put("contacts", contactsEntities);
    }

    @Override
    public R save(SaveContactsVO saveContactsVO) {
        String name = saveContactsVO.getName();
        String email = saveContactsVO.getEmail();

        if (email == null || email.equals("")) {
            return R.error("邮箱不能为空");
        }

        Integer currentUserId = CurrentUser.getCurrentUserId();
        ContactsEntity contactsEntity = new ContactsEntity();
        contactsEntity.setName(name);
        contactsEntity.setEmail(email);
        contactsEntity.setUserId(currentUserId);
        int insert = contactsDao.insert(contactsEntity);
        if (insert == 0) {
            return R.error("新增失败");
        }

        return R.ok();
    }

    @Override
    public R update(UpdateContactsVO updateContactsVO) {
        Integer contactsId = updateContactsVO.getContactsId();
        String email = updateContactsVO.getEmail();
        String name = updateContactsVO.getName();

        if(contactsId == null){
            return R.error("id不能为空");
        }

        ContactsEntity contactsEntity = this.baseMapper.selectById(contactsId);

        if(email != null && !email.equals("")){
            contactsEntity.setEmail(email);
        }

        if(name != null && !name.equals("")){
            contactsEntity.setName(name);
        }
        this.baseMapper.updateById(contactsEntity);
        return R.ok("更新成功");
    }

}